<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Expression;
class QuestionsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	
	public function saveQuestions($questions)
    {
		if($questions['type']==0){
			$subjectId=0;
		}else{
		   $subjectId=$questions['subject_id'];
		}
		$data = array(
			'course_id'				=> $questions['course_id'],
			'subject_id'			=> $subjectId,
			'question_name' 	  	=> $questions['question_name'], 	
			'image' 	  			=> "image", 	
			'option1' 	  			=> $questions['option1'], 	
			'option2' 	  			=> $questions['option2'], 	
			'option3' 	  			=> $questions['option3'], 	
			'option4' 	  			=> $questions['option4'], 	
			'level' 	  			=> $questions['level'], 	
			'year_of_experience' 	=> $questions['year_of_experience'], 	
			//'question_type' 		=> $questions['question_type'], 	
			'status' 				=> 1, 				
		);
		$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
    }
	
	//UPDATE
	
	public function upadteQuestions($questions)
    {
		if($questions['type']==0 && $questions['subject_id']==0){
			$subjectId=0;
		}else{
		   $subjectId=$questions['subject_id'];
		}
		$data = array(
			'course_id'				=> $questions['course_id'],
			'subject_id'			=> $subjectId,
			'question_name' 	  	=> $questions['question_name'], 	
			'image' 	  			=> "image", 	
			'option1' 	  			=> $questions['option1'], 	
			'option2' 	  			=> $questions['option2'], 	
			'option3' 	  			=> $questions['option3'], 	
			'option4' 	  			=> $questions['option4'], 	
			'level' 	  			=> $questions['level'], 	
			'year_of_experience' 	=> $questions['year_of_experience'], 	
			'status' 				=> 1, 				
		);
		$updatedData=$this->tableGateway->update($data, array('question_id' => $questions['question_id']));
		return 	$updatedData;
    }
	
	public function getQuestionslist($c_id,$value,$user_id,$level,$subject_id,$question_id){
		$start=0;
		$end=10;
		$select = $this->tableGateway->getSql()->select();
		$rand = new \Zend\Db\Sql\Expression('RAND()');
		$select->join('pedia_results', new Expression('pedia_results.question_id=pedia_questions.question_id and pedia_results.user_id="'.$user_id.'" and pedia_results.status="1"'),array('user_answer'),'left');
		$select->join('pedia_courses', new Expression('pedia_courses.course_id=pedia_questions.course_id'),array('year_of_experience'),'left');
		$select->where('pedia_questions.course_id="'.$c_id.'"');
		$select->where('pedia_questions.level="'.$level.'"');
		$select->where('pedia_questions.subject_id="'.$subject_id.'"');
		$select->where('pedia_questions.question_id!="'.$question_id.'"');
		$select->where('pedia_questions.year_of_experience=pedia_courses.year_of_experience');
		$select->where('pedia_questions.status="1"')->limit($end)->offset((int)$start)->order($rand);
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	
	//Get Single Questions list
	public function getSingQuestion($c_id,$qu_id,$user_id,$level,$subject_id,$preQuestion_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_results', new Expression('pedia_results.question_id=pedia_questions.question_id and pedia_results.user_id="'.$user_id.'" and pedia_results.status="1"'),array('user_answer'),'left');
		$select->join('pedia_courses', new Expression('pedia_courses.course_id=pedia_questions.course_id'),array('year_of_experience'),'left');
		$select->where('pedia_questions.course_id="'.$c_id.'"');
		$select->where('pedia_questions.level="'.$level.'"');
		$select->where('pedia_questions.subject_id="'.$subject_id.'"');
		$select->where('pedia_questions.question_id="'.$qu_id.'"');
		$select->where('pedia_questions.year_of_experience=pedia_courses.year_of_experience');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	
	public function getlevelList($cId,$subjectId){
		$select = $this->tableGateway->getSql()->select();
		$select->where('pedia_questions.course_id="'.$cId.'"');
		$select->where('pedia_questions.subject_id="'.$subjectId.'"');
		$select->where('pedia_questions.status="1"');
		$select->group('pedia_questions.level');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	
	public function getQuestion($question_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_answer', new Expression('pedia_answer.question_id=pedia_questions.question_id'),array('correct_answer'),'left');
		$select->where('pedia_questions.question_id="'.$question_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getTotalQuestions(){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_answer', new Expression('pedia_answer.question_id=pedia_questions.question_id'),array('correct_answer'),'left');
		$select->where('pedia_questions.subject_id!="0"');
		//$select->join('pedia_courses', new Expression('pedia_courses.course_id=pedia_questions.course_id'),array('course_name','course_id'),'left');
		//$select->join('pedia_subjects', new Expression('pedia_subjects.course_id=pedia_courses.course_id'),array('subject_id','subject_name'),'left');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getTotalInterviewQuestions($type){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_answer', new Expression('pedia_answer.question_id=pedia_questions.question_id'),array('correct_answer'),'left');
		$select->where('pedia_questions.subject_id="'.$type.'"');
		//$select->join('pedia_courses', new Expression('pedia_courses.course_id=pedia_questions.course_id'),array('course_name','course_id'),'left');
		//$select->join('pedia_subjects', new Expression('pedia_subjects.course_id=pedia_courses.course_id'),array('subject_id','subject_name'),'left');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function deleteQuestion($sub_id,$cou_id,$que_id)
    {	
	    $deleteStatus=$this->tableGateway->delete(array('question_id' => $que_id,'course_id'=>$cou_id, 'subject_id'=>$sub_id));
        return $deleteStatus;
	}
}